{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b73068da",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| default_exp cli"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27a7e6e2",
   "metadata": {},
   "source": [
    "# Command Line Tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acfbc502",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "from fastcore.utils import *\n",
    "from fastcore.script import call_parse, bool_arg\n",
    "from subprocess import check_output, run\n",
    "\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11d71cfc",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "@call_parse\n",
    "def railway_link():\n",
    "    \"Link the current directory to the current project's Railway service\"\n",
    "    j = json.loads(check_output(\"railway status --json\".split()))\n",
    "    prj = j['id']\n",
    "    idxpath = 'edges', 0, 'node', 'id'\n",
    "    env = nested_idx(j, 'environments', *idxpath)\n",
    "    svc = nested_idx(j, 'services', *idxpath)\n",
    "\n",
    "    cmd = f\"railway link -e {env} -p {prj} -s {svc}\"\n",
    "    res = check_output(cmd.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "586830f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def _run(a, **kw):\n",
    "    print('#', ' '.join(a))\n",
    "    run(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b84fd5ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "@call_parse\n",
    "def railway_deploy(\n",
    "    name:str, # The project name to deploy\n",
    "    mount:bool_arg=True # Create a mounted volume at /app/data?\n",
    "):\n",
    "    \"\"\"Deploy a FastHTML app to Railway\"\"\"\n",
    "    nm,ver = check_output(\"railway --version\".split()).decode().split()\n",
    "    assert nm=='railwayapp', f'Unexpected railway version string: {nm}'\n",
    "    if ver2tuple(ver)<(3,8): return print(\"Please update your railway CLI version to 3.8 or higher\")\n",
    "    cp = run(\"railway status --json\".split(), capture_output=True)\n",
    "    if not cp.returncode:\n",
    "        print(\"Checking deployed projects...\")\n",
    "        project_name = json.loads(cp.stdout.decode()).get('name')\n",
    "        if project_name == name: return print(\"This project is already deployed. Run `railway open`.\")\n",
    "    reqs = Path('requirements.txt')\n",
    "    if not reqs.exists(): reqs.write_text('python-fasthtml')\n",
    "    _run(f\"railway init -n {name}\".split())\n",
    "    _run(f\"railway up -c\".split())\n",
    "    _run(f\"railway domain\".split())\n",
    "    railway_link.__wrapped__()\n",
    "    if mount: _run(f\"railway volume add -m /app/data\".split())\n",
    "    _run(f\"railway up -c\".split())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "474e14b4",
   "metadata": {},
   "source": [
    "## Export -"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d211e8e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#|hide\n",
    "import nbdev; nbdev.nbdev_export()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b1d43a0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
